603 數字排序
list3 = [] for i in range(10): num = eval(input().strip()) list3.append(num) list3.sort() print(list3[-1], list3[-2], list3[-3])
append函數增加數值到陣列最右邊一格, 陣列.sort 可把陣列數值由小到大排序, 再用負索引(negative index)把數值由大到小印出
703 串列數組轉換
list2 = [] num2 = int(input().strip()) while num2 != -9999: list2.append(num2) num2 = int(input().strip()) tuple2 = tuple(list2) print(tuple2) print("Length:",len(tuple2)) print("Max:",max(tuple2)) print("Min:",min(tuple2)) print("Sum:",sum(tuple2))
將list轉為tuple, 再用len(), max(), min(), sum()函數來做數值分析
802 字元對應
word = (input().strip()) total = 0 for i in range(len(word)): print("ASCII code for '%s' is %d" %(word[i], ord(word[i]))) total += ord(word[i]) print(total)
len(字串),測出的長度做for迴圈重複的次數上限, ord()則可找出字元對應的ASCII碼
參考資料:TQC+ Python 3